/**
 * 暂存,提交按钮及创建json字符串方法合并js文件
 * @author HuangS
 * @version 1.0
 */
var sign = false;
var signnum=false;
var adopt=false;
/**
 * 创建json字符串
 */
function creatJsonObj(){
    var answerArray = new Array();
    //获取单选题答案
    var selectProblemInfo = $("#selectProblemInfo");
    var select = selectProblemInfo.find('input:hidden');
    var verificationText=$("#verificationText").val();
    if(verificationText != undefined){
    	//获取二维码
    	var getVverification=$.ajax({url:"../userquestionnaire/getEWM.do",async:false}).responseText;
    	if(verificationText!=getVverification){
    		adopt=true;
    	}
    }
    var selectLen = 0;
    if (select != null) {
        selectLen = select.length;
    }
   
    for (var j = 1; j <= selectLen; j++) {
    	var headSelect = $(select[j-1]).attr("isMust");//是否必填
        var selectArray = selectProblemInfo.find("input[name='pdt_select" + j + "']:checked");
        var answerObj = new Object();
        answerObj.itemId = select[j - 1].value;
        answerObj.itemType='danxt';
        var array = new Array();
        $.each(selectArray, function(i, item){
            array.push(item.value);
        });
        answerObj.studentResult = array;
        answerArray.push(answerObj);
        if(headSelect=='必填'){
	        if (selectArray == null || selectArray.length == 0) {
	            sign = true;
	        }
        }
    };
    
    //获取多选题答案
    var multipleSelectProblemInfo = $("#multipleSelectProblemInfo");
    var multiple = multipleSelectProblemInfo.find('input:hidden');
    var multipleLen = 0;
    if (multiple != null) {
        multipleLen = multiple.length;
    }
    for (var j = 1; j <= multipleLen; j++) {
    	var headSelect = $(multiple[j-1]).attr("isMust");
        var multipleArray = multipleSelectProblemInfo.find("input[name='pdt_multiple" + j + "']:checked");
        var answerObj = new Object();
        answerObj.itemId = multiple[j - 1].value;
        answerObj.itemType='duoxt';
        var array = new Array();
        $.each(multipleArray, function(i, item){
        	var itemvalue=item.value;
        	if($(item).attr("objectiveTopic")=='1'){
        		itemvalue += $(item).closest("tr").next("tr").find("textarea").val();
        	}
            array.push(itemvalue);
        });
        answerObj.studentResult = array;
        answerArray.push(answerObj);
        if(headSelect=='必填'){
	        if (multipleArray == null || multipleArray.length == 0) {
	            sign = true;
	        }
        }
    };
    
    //获取问答题答案
    var judgeProblemInfo = $("#essayQuestionInfo");
    var judge = judgeProblemInfo.find('input:hidden');
    var judgeLen = 0;
    if (judge != null) {
        judgeLen = judge.length;
    }
    for (var j = 1; j <= judgeLen; j++) {
    	var headSelect = $(judge[j-1]).attr("isMust");
    	var headNum = $(judge[j-1]).attr("wordNum");
        var judgeArray = judgeProblemInfo.find("textarea[name='pdt_essay_question_answer" + j + "']");
        var answerObj = new Object();
        answerObj.itemId = judge[j - 1].value;
        answerObj.itemType='wendt';
        var array = new Array();
        $.each(judgeArray, function(i, item){
            array.push(item.value);
        });
        answerObj.studentResult = array;
        answerArray.push(answerObj);
        if(headSelect=='必填'){
	        if (judgeArray == null || judgeArray[0].value.length == 0) {
	            sign = true;
	        }else{
	           //判断字数
	        	if(headNum!=null&&judgeArray[0].value.length>headNum){
	        		signnum=true;
	        	}
	        }
        }else{
        	//判断字数
        	if(headNum!=null&&judgeArray[0].value.length>headNum){
        		signnum=true;
        	}
        }
    };
    
    var answerStr = JSON.stringify(answerArray);
    return answerStr;
}

function sub(e){
	disBtn();
	subBut(e);
}

function subBut(e){
	 var answerStr = creatJsonObj();
	    if (e) {
	        if (sign) {
				alert("您还有必填题目未答，答完所有题目后才可以提交问卷。");
				sign = false;
				redisBtn();
	            return;
	        }else if(signnum){
	        	alert("问答题超出字数限制");
				signnum=false;
				redisBtn();
				return;
	        }else if(adopt){
	        	alert("验证码输入错误");
	        	changeImg();
	        	adopt=false;
				redisBtn();
				return;
	        } else {
	            if (!confirm("提交后不可再对问卷进行更改，您确定要提交吗？")) {
	            	redisBtn();
	                return;
	            }
	        }
	    }
	    timeStop = true;
	    $.ajax({
	        type: "POST", // 也可以改成GET
	        //async: false, //
	        dataType:"json",
	        //cache: false, //
	        url: basePath + "module/useranswerlink/sumitPaper.json?rand="+parseInt(Math.random()* (2000000 - 5 + 1) + 5),
	        data: {
	        	searchQuestionnaireId: $("#searchQuestionnaireId").val(),
	        	searchClassId: $("#searchClassId").val(),
	            searchStudentAnswerInfoStr: answerStr,
	            "springmvc.TOKEN": token
	        },
	        success: function(data){
	            if (data.success != null) {
	            	if (data.success==true){
	            		//需要刷新父页面
	            		//window.opener=null;
	            		alert("评估完成，感谢您的参与");
	            		backlist($("#searchClassId").val(),$("#searchUserID").val(),$("#searchUserLoginId").val());
	    				//window.open("","_self");
	    				//window.close();
	            	}else{
		                alert('提交失败');
	                }
	            }
	            else {
	            	alert('提交失败');
	            }
	        }
	    });
}


/**
 * 提交后使按钮失效
 * @author WangWB
 */
function disBtn(){
    document.getElementById("jiaojuanbut").disabled="disabled";
    window.onbeforeunload=null;
}

/**
 * 提交后使按钮生效
 * @author WangWB
 */
function redisBtn(){
    document.getElementById("jiaojuanbut").disabled="";
    window.onbeforeunload = onbeforeunload_handler;
}

/**
 * 考试结果页关闭按钮事件
 * @author HuangS
 */
function closeBtn(){
    window.close();
    parent.closeExamDiv('examf');
}

function paperReset(){
	$('#paperform')[0].reset();
}
function backlist(searchClassId,searchUserId,searchUserLoginId){
	window.open(basePath + "module/userquestionnaire/userLogin.do?searchClassId="+searchClassId+"&searchUserId="+searchUserId+"&searchUserLoginId="+searchUserLoginId,"_top");
}
